//
//  CustomView.m
//  FaceRecognition
//
//  Created by zz on 16/12/2.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "CustomPhotoView.h"

@implementation CustomPhotoView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        CGFloat alpha = 0.6;
        
        CGFloat w = frame.size.width;
        CGFloat h = frame.size.height;
        
        //中间透明层
        _clear = [[UIView alloc] initWithFrame:CGRectMake(40, 0, w - 80, w - 80)];
        _clear.backgroundColor = [UIColor clearColor];
        _clear.center = CGPointMake(w / 2, h * 0.45);
        [self addSubview:_clear];
        
        //上view
        UIView *top = [[UIView alloc] init];
        top.frame = CGRectMake(0, 0, w, h * 0.45 - _clear.frame.size.width * 0.5);
        top.backgroundColor = [UIColor blackColor];
        top.alpha = alpha;
        [self addSubview:top];
        
        //下view
        UIView *bottom = [[UIView alloc] init];
        bottom.frame = CGRectMake(0, CGRectGetMaxY(_clear.frame), w, h - CGRectGetMaxY(_clear.frame));
        bottom.backgroundColor = [UIColor blackColor];
        bottom.alpha = alpha;
        [self addSubview:bottom];
        
        //左view
        UIView *left = [[UIView alloc] init];
        left.frame = CGRectMake(0, CGRectGetMaxY(top.frame), 40, w - 80);
        left.backgroundColor = [UIColor blackColor];
        left.alpha = alpha;
        [self addSubview:left];
        
        //右view
        UIView *right = [[UIView alloc] init];
        right.frame = CGRectMake(CGRectGetMaxX(_clear.frame), CGRectGetMaxY(top.frame), 40, w - 80);
        right.backgroundColor = [UIColor blackColor];
        right.alpha = alpha;
        [self addSubview:right];
        
        //切换摄像头按钮
        UIButton *switchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        switchButton.frame = CGRectMake(w - 23 - 30, 33, 30, 24);
        [switchButton setImage:[UIImage imageNamed:@"switch_camera_"] forState:UIControlStateNormal];
        [switchButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
        switchButton.tag = 1;
        [self addSubview:switchButton];
        
        //取消
        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelButton.frame = CGRectMake(w - 20 - 60, h - 45 - 30, 60, 30);
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:22];
        [cancelButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
        cancelButton.tag = 2;
        [self addSubview:cancelButton];
        
        //拍照
        UIButton *takeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        takeButton.frame = CGRectMake((w - 67) / 2, h - 36 - 52.5 , 67, 52.5);
        [takeButton setImage:[UIImage imageNamed:@"camera_btn_"] forState:UIControlStateNormal];
        takeButton.tag = 3;
        [takeButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:takeButton];
        
        //提示label
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(0,CGRectGetMaxY(_clear.frame) + 20,w,22);
        label.text = @"请将脸部置于框内";
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:22];
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
        
    }
    
    return self;
}

- (void)cancelAction:(UIButton *)sender{
    
    switch (sender.tag) {
            
        case 1: //切换摄像头
            
            if ([_cameraDelegate respondsToSelector:@selector(switchCameraAction:)]) {
                
                [_cameraDelegate switchCameraAction:sender];
            }
            
            break;
            
        case 2: //取消
        
            if ([self.delegate respondsToSelector:@selector(cancelCameraAction:)]) {
                
                [self.delegate cancelCameraAction:sender];
            }
            
            break;
            
        case 3: //拍照
            
            if ([self.cameraDelegate respondsToSelector:@selector(takePhotoAction:)]) {
                
                [self.cameraDelegate takePhotoAction:sender];
            }
            
            break;
            
        default:
            break;
    }
}

@end
